// pages/detail/detail.js
var imgUrls = [];
var detailImg = [];
var goodsId = null;
var goods = null;
const app = getApp()
const db = wx.cloud.database();
var likeItem = null;
var likeItemLength = null;
const openid = app.globalData.openid
var now_count = 1;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLike: false,
    showDialog: true,
    goods: null,
    indicatorDots: true, //是否显示面板指示点
    autoplay: true, //是否自动切换
    interval: 3000, //自动切换时间间隔,3s
    duration: 1000, //  滑动动画时长1s
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    goodsId = options.goodsId;
    console.log('goodsId:' + goodsId);
    //加载商品详情
    that.goodsInfoShow()
    //收藏判断
    that.like()
  },
  toCar: function () {
    wx.switchTab({
      url: '../cart/cart',
    })
  },
  goodsInfoShow: function() {
    var that = this;
    db.collection('goodsInfo').where({
        id: goodsId
      })
      .get({
        success(res) {
          var goodsItem = res.data[0];
          var details = goodsItem.details.split(";");
          for (var i = 0; i < details.length; i++) {
            detailImg[i] = details[i];
          }
          for (var j = 0; j < goodsItem.shopGoodsImageList.length; j++) {
            imgUrls[j] = goodsItem.shopGoodsImageList[j].imgUrl;
          }

          goods = {
            imgUrls: imgUrls,
            title: goodsItem.name,
            price: goodsItem.price,
            privilegePrice: goodsItem.privilegePrice,
            detailImg: detailImg,
            imgUrl: goodsItem.imgUrl,
            buyRate: goodsItem.buyRate,
            goodsId: goodsId,
            count: goodsItem.count,
            totleMoney: goodsItem.price,
            c_count: 1,
            isSelect: false
          }
          that.setData({
            goods: goods
          })
        }
      })
  },
  like: function() {
    var that = this;
    console.log(app.globalData.openid)
    db.collection('like').where({
        goodsId: goodsId,
        _openid: app.globalData.openid
      })
      .get({
        success(res) {
          console.log(res)
          likeItemLength = res.data.length
          if (likeItemLength == 0) {
            that.setData({
              isLike: false
            })
          } else {
            likeItem = res.data[0];

            that.setData({
              isLike: likeItem.isLike
            })
          }

        }
      })
  },
  addLike: function() {
    var that = this;
    that.like();
    if (likeItemLength == 0) {
      console.log('没有数据')
      
      db.collection('like').add({

        // data 字段表示需新增的 JSON 数据
        data: {
          // _id: 'todo-identifiant-aleatoire', // 可选自定义 _id，在此处场景下用数据库自动分配的就可以了
          goodsId: goodsId,
          isLike: true
        },
        success(res) {
          console.log(res)
          wx.showToast({
            title: '收藏成功',
            icon: 'success',
            duration: 2000
          })
          that.setData({
            isLike: true
          })
        }
      })
    } else {
      if (likeItem.isLike) {
        var _id = likeItem._id;
        console.log('true')
        //删除
        db.collection('like').doc(_id).remove({
          success(res) {
            wx.showToast({
              title: '取消成功',
              icon: 'none',
              duration: 2000
            })
            that.setData({
              isLike: false
            })
          }
        })

      }
    }
  },

  //数量+
  addCount: function (e){
    var index = e.target.dataset.index;
    var count = this.data.goods.count;
    if (this.data.goods.c_count < count){
      this.data.goods.c_count++;
    }else{
      wx.showToast({
        title: '不能再减啦',
        icon: 'none',
        duration: 2000
      })
    }
    this.setData({
      goods: this.data.goods
    });
    this.priceCount()
    now_count = this.data.goods.c_count
    console.log(this.data.goods)
  },
  delCount: function (e) {
    var index = e.target.dataset.index;
    var count = this.data.goods.count;
    if (this.data.goods.c_count > 1) {
      this.data.goods.c_count--;
    } else {
      wx.showToast({
        title: '不能再减啦',
        icon: 'warn',
        duration: 2000
      })
    }
    this.setData({
      goods: this.data.goods
    });
    now_count = this.data.goods.c_count
    this.priceCount()
  },
  priceCount: function(){
    this.data.goods.totleMoney = this.data.goods.price * this.data.goods.c_count;
    this.setData({
      goods: this.data.goods
    })
  },

  //关闭购物车弹窗
  toggleDialog: function(){
    this.setData({
      showDialog: !this.data.showDialog
    })
  },
  closeDialog: function(){
    this.setData({
      showDialog: false
    })
  },

  //加入购物车
  addCar: function(){
    var shopCar = wx.getStorageSync('shopCar') || [] ;
    if(shopCar.length > 0){
      //有数据
      for(var i in shopCar){
        if(shopCar[i].goodsId == goodsId){
          //有该产品,则数量+1
          shopCar[i].c_count = shopCar[i].c_count + now_count;
          //更新缓存
          wx.setStorageSync('shopCar', shopCar)
          wx.showToast({
            title: '加入购物车成功！',
            icon: 'success',
            duration: 2000
          });
          this.closeDialog();
          return
        }
      }
      shopCar.push(this.data.goods)
    }else{
      //无数据
      shopCar.push(this.data.goods)
      
    }
    wx.setStorageSync('shopCar', shopCar)
    wx.showToast({
      title: '加入购物车成功！',
      icon: 'success',
      duration: 2000
    });
    this.closeDialog();
  }
})